package com.pay.paytoken.activity.lock;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.pay.paytoken.R;
import com.pay.paytoken.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import appUtils.ToastUtil;
import butterknife.Bind;
import butterknife.OnClick;

/**
 * 修改时输入旧密码的界面
 * Created by Administrator on 2017/7/21.
 */

public class DigitalConfirmLockActivity extends BaseActivity implements TextWatcher {
    private static final int REQUEST_FOCUS = 0x1;
    private static final int CLEAR_DIGITAL = 0x1 << 2;
    @Bind(R.id.layout_title_textName)
    TextView mTitleTv;
    @Bind(R.id.layout_title_ivSecond)
    TextView mRightTv;
    @Bind(R.id.activity_set_lock_digital_remark_tv)
    TextView mRemarkTv;
    @Bind(R.id.activity_set_lock_digital_et)
    EditText mDigitalEt;
    @Bind(R.id.num_1)
    TextView mNum1;
    @Bind(R.id.num_2)
    TextView mNum2;
    @Bind(R.id.num_3)
    TextView mNum3;
    @Bind(R.id.num_4)
    TextView mNum4;
    @Bind(R.id.num_5)
    TextView mNum5;
    @Bind(R.id.num_6)
    TextView mNum6;
    private List<TextView> mNums = new ArrayList<>();
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REQUEST_FOCUS:
                    mDigitalEt.requestFocus();
                    break;
                case CLEAR_DIGITAL:
                    clearNumsBG();
                    mDigitalEt.setText("");
                    break;

            }
        }
    };

    @Override
    public int setView() {
        return R.layout.activity_set_lock_digital;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {
        mRightTv.setVisibility(View.INVISIBLE);
        mTitleTv.setText(R.string.unlock_pass);
        mRemarkTv.setText(R.string.please_enter_old_pass);
        mDigitalEt.addTextChangedListener(this);
        mNums.add(mNum1);
        mNums.add(mNum2);
        mNums.add(mNum3);
        mNums.add(mNum4);
        mNums.add(mNum5);
        mNums.add(mNum6);
        mHandler.sendEmptyMessage(CLEAR_DIGITAL);
        mHandler.sendEmptyMessage(REQUEST_FOCUS);
        //设置Edittext的监听，使Edittext获取焦点的时候调起键盘
        mDigitalEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        });
    }

    private void clearNumsBG() {
        for (TextView num : mNums) {
            num.setBackgroundResource(R.drawable.background_digital_lock_circle_unselected);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        digitalChange(s.length());
    }

    /**
     * 监听Edittext的输入，长度大于6的时候截断方法，设置密码的置灰显示，当位数为6的时候验证密码和已保存数字密码是否一致
     * @param length
     */
    private void digitalChange(int length) {
        if (length > 6) {
            return;
        }
        for (int i = 0; i < length; i++) {
            mNums.get(i).setBackgroundResource(R.drawable.background_digital_lock_circle_selected);
        }
        if (length == 6) {
            String mPass = mDigitalEt.getText().toString();
            if (mPass.equals(LockManager.getInstance().getDigitalPass())) {
                LockManager.getInstance().setDigitalLock(this);
                finish();
            } else {
                ToastUtil.showLong(R.string.pass_error);
                mHandler.sendEmptyMessageDelayed(CLEAR_DIGITAL, 500);
            }
        } else {
            mNums.get(length).setBackgroundResource(R.drawable.background_digital_lock_circle_unselected);
        }
    }

    @OnClick(R.id.layout_title_iv_left)
    protected void cancel() {
        finish();
    }
}
